package com.kaiyidiannao.model.req.model;

import android.text.TextUtils;
import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.AuctionDetail;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONObject;

import java.util.HashMap;

/**
 *  拍拍乐详情页面 异步请求
 */
public class GetAuctionShow extends BaseCommand {

    private String itemid; //当前分类编号
    private String userid;
    private int catid;
    private int page =  DataCenter.LISTVIEW_DEFAULT_PAGE; //当前页码
    private int  order = 0;//排序方式
    private String TAG = "GetAuctionShow";



    public GetAuctionShow(String itemid, int catid) {
        this.itemid = itemid;
        this.catid = catid;
    }

    public GetAuctionShow(String userid, String itemid, int catid) {
        this.itemid = itemid;
        this.userid = userid;
        this.catid = catid;
    }


    @Override
    public String getRequestUrl() {
        return DataCenter.GET_AUCTION_SHOW;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("itemid", itemid);
        paramters.put("catid", catid + "");
        if (!TextUtils.isEmpty(userid)) {
            paramters.put("userid", userid);
        }
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.getByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetAuctionShow.yes()");

        try {
            Log.d(TAG, result.object.toString());

            JSONObject jsonObject = new JSONObject(result.object.toString());
            JSONObject obj = jsonObject.getJSONObject("data");

            DataCenter.auctionShow = null;
            AuctionDetail bean = JsonUtil.jsonToObject(obj.toString(),AuctionDetail.class);

            DataCenter.auctionShow = bean;
        } catch (Exception e) {
            e.printStackTrace();
        }

        DataCenter.getInstance().callBacks(DataCenter.flag.GET_AUCTION_SHOW_SUCCESS);
    }

    @Override
    public void no(Result result) {
        // super.no(result);
        Log.d(DataCenter.LOG_ERROR, "GetAuctionShow.no()");
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_AUCTION_SHOW_FAILED);

    }
}
